package com.wyz.mybatislogtosql;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;

/**
 * 视图
 */
public class MyBatisLogToSqlView extends JFrame {

    private final JTextArea sqlTextArea;

    public MyBatisLogToSqlView() throws HeadlessException {
        super("mybatis log to sql");
        setSize(400, 600);
        setResizable(false);
        setLocation(400, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        setContentPane(panel);
        panel.setLayout(null);

        JTextArea logTextArea = new JTextArea();
        logTextArea.setLineWrap(true);
        JScrollPane logScrollPane = new JScrollPane(logTextArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        panel.add(logScrollPane);


        sqlTextArea = new JTextArea();
        sqlTextArea.setLineWrap(true);
        sqlTextArea.setEditable(false);
        JScrollPane sqlScrollPane = new JScrollPane(sqlTextArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        panel.add(sqlScrollPane);

        JPanel filePanel = new JPanel();
        panel.add(filePanel);
        JButton selectionFile = new JButton("choose file");
        JTextField textField = new JTextField("D:\\out.log",25);// 文本域
        JLabel filter = new JLabel("regex filter:");
        JTextField filterTextField = new JTextField(".*",25);// 文本域
        selectionFile.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                chooser.showDialog(new JLabel(), "choose file");
                File file = chooser.getSelectedFile();
                if (file!=null){
                    try{
                        MyBatisLogToSqlUtils.handleLogFile(file,new File(textField.getText()),filterTextField.getText());
                        JOptionPane.showMessageDialog(null,"handle success", "", JOptionPane.INFORMATION_MESSAGE);
                    }catch (Exception ex){
                        ex.printStackTrace();
                        JOptionPane.showMessageDialog(null,ex.getMessage(), "", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });
        filePanel.add(selectionFile);
        filePanel.add(textField);
        filePanel.add(filter);
        filePanel.add(filterTextField);



        logTextArea.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                try {
                    sqlTextArea.getDocument().remove(0, sqlTextArea.getDocument().getLength());
                    String log = e.getDocument().getText(0, e.getLength());
                    String convert = MyBatisLogToSqlUtils.convert(log.split("\n"),filterTextField.getText());
                    sqlTextArea.getDocument().insertString(0, convert, null);
                } catch (BadLocationException badLocationException) {
                    badLocationException.printStackTrace();
                }
            }

            public void removeUpdate(DocumentEvent e) {

            }

            public void changedUpdate(DocumentEvent e) {
            }
        });

        selectionFile.setBounds(0,0,100,100);
        logScrollPane.setBounds(0, 0, 400, 200);
        sqlScrollPane.setBounds(0, 200, 400 ,200);
        filePanel.setBounds(0, 400, 400, 200);
        setVisible(true);
    }



    public static void main(String[] args) {
        MyBatisLogToSqlView view = new MyBatisLogToSqlView();
    }
}